package com.service.impl;

import com.dao.UserDao;
import com.dto.BaseDto;
import com.dto.dataenum.MessageEnum;
import com.entity.User;
import com.entity.UserExample;
import com.page.PageInfo;
import com.service.BaseService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl extends BaseService implements UserService {
	@Autowired
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	@Override
	public BaseDto getAllUsers(PageInfo pageInfo) throws SQLException {
		BaseDto dto = new BaseDto();
		List<User> users = userDao.getAllUsers(pageInfo);

		msg = Optional.ofNullable(users).isPresent()? MessageEnum.SELECT_COMMENT_SUCCESS.getMsg():MessageEnum.SELECT_COMMENT_FAIL.getMsg();
		code = Optional.ofNullable(users).isPresent()?MessageEnum.SELECT_COMMENT_SUCCESS.getCode():MessageEnum.SELECT_COMMENT_FAIL.getCode();
		dto.setCode(code);
		dto.setMsg(msg);

		dto.settList(users);
		return dto;
	}
	@Override
	public BaseDto getUserById(int uid) throws SQLException{
		BaseDto dto = new BaseDto();
		User user = userDao.getUserById(uid);
		msg = Optional.ofNullable(user).isPresent()? MessageEnum.SELECT_COMMENT_SUCCESS.getMsg():MessageEnum.SELECT_COMMENT_FAIL.getMsg();
		code = Optional.ofNullable(user).isPresent()?MessageEnum.SELECT_COMMENT_SUCCESS.getCode():MessageEnum.SELECT_COMMENT_FAIL.getCode();
		dto.setCode(code);
		dto.setMsg(msg);
		dto.setT(user);
		return dto;
	}
	@Override
	public BaseDto insertUser(User user) throws IllegalArgumentException, IllegalAccessException, SQLException{
		BaseDto dto = new BaseDto();
		int result = 0;
		result = userDao.insertUser(user);
		msg = Optional.ofNullable(result).isPresent()? MessageEnum.SELECT_COMMENT_SUCCESS.getMsg():MessageEnum.SELECT_COMMENT_FAIL.getMsg();
		code = Optional.ofNullable(result).isPresent()?MessageEnum.SELECT_COMMENT_SUCCESS.getCode():MessageEnum.SELECT_COMMENT_FAIL.getCode();
		dto.setCode(code);
		dto.setMsg(msg);
		dto.setT(result);
		return dto;
	}
	@Override
	public BaseDto updateUser(User user) throws IllegalArgumentException, IllegalAccessException, SQLException,NoSuchFieldException, SecurityException{
		BaseDto dto = new BaseDto();
		int result = 0;
		result = userDao.updateUser(user);
		msg = Optional.ofNullable(result).isPresent()? MessageEnum.SELECT_COMMENT_SUCCESS.getMsg():MessageEnum.SELECT_COMMENT_FAIL.getMsg();
		code = Optional.ofNullable(result).isPresent()?MessageEnum.SELECT_COMMENT_SUCCESS.getCode():MessageEnum.SELECT_COMMENT_FAIL.getCode();
		dto.setCode(code);
		dto.setMsg(msg);
		dto.setT(result);
		return dto;
	}
	public BaseDto delateUser(int uid){
		BaseDto dto = new BaseDto();
		int result = 0;
		result = userDao.delateUser(uid);
		msg = Optional.ofNullable(result).isPresent()? MessageEnum.SELECT_COMMENT_SUCCESS.getMsg():MessageEnum.SELECT_COMMENT_FAIL.getMsg();
		code = Optional.ofNullable(result).isPresent()?MessageEnum.SELECT_COMMENT_SUCCESS.getCode():MessageEnum.SELECT_COMMENT_FAIL.getCode();
		dto.setCode(code);
		dto.setMsg(msg);
		dto.setT(result);
		return dto;
	}

}
